﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebControlApp.UI
{
    class Presenter
    {
        WebControlApp.Iview m_View;
        WebControlApp.UI.Model m_Model;

        public Presenter(WebControlApp.Iview view)
        {
            m_View = view;
            m_Model = new WebControlApp.UI.Model(this);
        }

        public void Keydown(Keys KeyData)
        {
            if (m_Model.timer1.Enabled == false)
            {
                if (KeyData == Keys.W)
                {
                    //Forward
                    m_Model.PacketData[2] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "F";
                    m_Model.SendData();
                }
                else if (KeyData == Keys.S)
                {
                    //Reverse
                    m_Model.PacketData[2] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "R";
                    m_Model.SendData();
                }
                else if (KeyData == Keys.A)
                {
                    //Piviot Right
                    m_Model.PacketData[2] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "L";
                    m_Model.SendData();

                }
                else if (KeyData == Keys.D)
                {
                    //Pivot Left
                    m_Model.PacketData[2] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "R";
                    m_Model.SendData();
                }
                else if (KeyData == Keys.LShiftKey)
                {
                    if (m_Model.AutoBrake == true)
                    {
                        m_Model.AutoBrake = false;
                    }
                    else if (m_Model.AutoBrake == false)
                    {
                        m_Model.AutoBrake = true;
                    }
                }
                else if (KeyData == Keys.Q)
                {
                    //nuge Right
                    byte temp = m_Model.PacketData[1];
                    m_Model.PacketData[1] = (byte)(65);
                    m_Model.PacketData[2] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "R";
                    m_Model.SendData();
                    m_Model.PacketData[1] = temp;
                }
                else if (KeyData == Keys.E)
                {
                    //nuge Left
                    byte temp = m_Model.PacketData[1];
                    m_Model.PacketData[1] = (byte)(65);
                    m_Model.PacketData[2] = (byte)(90 - m_Model.MaxSpeed);
                    m_Model.PacketData[3] = (byte)(122 - m_Model.MaxSpeed);
                    m_Model.LogView = m_Model.LogView + "Y";
                    m_Model.SendData();
                    m_Model.PacketData[1] = temp;
                }
                else if (KeyData == Keys.X)
                {
                    //Break
                    m_Model.PacketData[2] = (byte)(61);
                    m_Model.PacketData[3] = (byte)(61);
                    m_Model.PacketData[8] &= 0xf7;
                    m_Model.AutoBrake = true;
                    m_Model.LogView = m_Model.LogView + "B";
                    m_Model.SendData();
                }


                m_Model.timer1.Enabled = true;
                m_Model.timer1.Start();
            }
        }

        public void forward()
        {             //Forward
            m_Model.PacketData[2] = (byte)(90 - m_Model.MaxSpeed);
            m_Model.PacketData[3] = (byte)(90 - m_Model.MaxSpeed);
            m_Model.SendData();
        }

        public void reverse()
        {
            //Reverse
            m_Model.PacketData[2] = (byte)(122 - m_Model.MaxSpeed);
            m_Model.PacketData[3] = (byte)(122 - m_Model.MaxSpeed);
            m_Model.SendData();
        }

        public void PiviotLeft()
        {
            //Piviot Left
            m_Model.PacketData[2] = (byte)(122 - m_Model.MaxSpeed);
            m_Model.PacketData[3] = (byte)(90 - m_Model.MaxSpeed);
            m_Model.SendData();
        }

        public void PiviotRight()
        {
            //Pivot Right
            m_Model.PacketData[2] = (byte)(90 - m_Model.MaxSpeed);
            m_Model.PacketData[3] = (byte)(122 - m_Model.MaxSpeed);
            m_Model.SendData();
        }

        public void Stop()
        {
            //Stop
            m_Model.PacketData[2] = 35;
            m_Model.PacketData[3] = 35;
            m_Model.SendData();
        }

        public void ChangeSyncTime(int value)
        {
            m_View.TimerLength = (value*62.5).ToString();
            m_Model.SyncTimer.Interval = ((int)(value * 62.5));
            m_Model.PacketData[1] = (byte)(m_Model.TimerLength + 64);
        }

        public void ChangedSpeed(int value)
        {
            m_Model.MaxSpeed = value;
            m_View.MaxSpeed = value.ToString();
        }

        public void Keyup()
        {
            //Stop
            m_Model.PacketData[2] = 33;
            m_Model.PacketData[3] = 33;
            m_Model.LogView = m_Model.LogView + "S";
            m_Model.SendData();
        }

        public void SensorOverRide()
        {
            m_Model.SensorOverride = m_View.SensorOverride;
        }

        public void Calibrate()
        {
            //Calibrate
            m_Model.Calibrate = m_View.Calibrate;
        }

        public void AutoBrake()
        {
            //AutoBrake
            m_Model.AutoBrake = m_View.AutoBrake;
        }

        public void Connect()
        {
            m_Model.Connect();
        }

        public void Dissconnect()
        {
            m_Model.Dissconnect();
        }

        public void Network()
        {
           // private Network NetworkForm;
           // NetworkForm = new Network(currentVideoConnection, currentCommandConnection);
           // NetworkForm.frm1 = this;
          //  NetworkForm.BringToFront();
          //  NetworkForm.Show();
        }
    }
}
